package com.springStudy.core.environment;

import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName :EnvironmentTest
 * @Description
 * @Author away
 * @Date 2019/7/31
 */
public class EnvironmentTest {
    //org.springframework.core.env.PropertySourcesPropertyResolver.propertySources  存放所有的properties属性
    // 1.systemEnvironment 放的是这台电脑上面的环境变量，path里面的东西，安装的软件这些
    // 2.systemProperties 这个方这个进程的属性，java启动的参数这些

    //org.springframework.util.PropertyPlaceholderHelper.parseStringValue() 方法用来从org.springframework.core.env.PropertySourcesPropertyResolver.propertySources 读取属性 如果没有找到则返回:后面的值 ${:}分隔的字符串

//    org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString 作为一个方法作为 org.springframework.util.PropertyPlaceholderHelper.parseStringValue()里面的placeholderResolver 的参数

// org.springframework.util.PropertyPlaceholderHelper.parseStringValue() 这个方法不断递归，保证多层解析

    /**
     * 获取环境变量
     */
    @Test
    public void test01() {
        System.setProperty("ylwTest", "测试下");
        ApplicationContext context = new ClassPathXmlApplicationContext();
        String value1 = context.getEnvironment().resolvePlaceholders("${key:defaultValue}");
        String value2 = context.getEnvironment().resolvePlaceholders("${ylwTest:defaultValue}");
        System.out.println(value1);
        System.out.println(value2);

        String value3 = context.getEnvironment().getProperty("ylwTest");
        System.out.println(value3);
    }

    /**
     * 从xml中解析bean
     */
    @Test
    public void test02() throws IOException {
        System.setProperty("spring.profiles.active", "dev");//org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions

        String location = "classpath*:META-INF/application-*.xml";


        ApplicationContext context = new ClassPathXmlApplicationContext();
        ((ClassPathXmlApplicationContext) context).setConfigLocation(location);
        BeanDefinitionRegistry registry = new DefaultListableBeanFactory(null);

        BeanDefinitionReader read = new XmlBeanDefinitionReader(registry);

//        read.loadBeanDefinitions(new String[]{location});
        PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();//
        read.loadBeanDefinitions(patternResolver.getResources(location));

        String[] names = registry.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }

//        String[] names = context.getBeanDefinitionNames();
//        for(String name: names){
//            System.out.println(name);
//        }

//        FileSystemResource resource = new FileSystemResource("D:\\ideaProject\\springStudy\\src\\test\\resources\\META-INF\\application-service-test.xml");
//        read.loadBeanDefinitions(resource);

    }


/*
org.springframework.beans.factory.support.DefaultListableBeanFactory.beanDefinitionMap 对象存放了所有的bean对象

//解析local文件
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(java.lang.String...)



org.springframework.beans.factory.xml.XmlBeanDefinitionReader.resourcesCurrentlyBeingLoaded 放了配置的xml文件


 */


    @Test
    public void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        System.setProperty("ylwTest", "测试下");
        StandardEnvironment environment = new StandardEnvironment();
        //PropertyResolver 解析占位符
        System.out.println(environment.resolvePlaceholders("${key:defaultValue}"));
        System.out.println(environment.resolvePlaceholders("${ylwTest:defaultValue}"));
        System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
        String[] activeProfiles = environment.getActiveProfiles();
        System.out.println(activeProfiles);
//        Method doGetActiveProfiles = environment.getClass().getMethod("doGetActiveProfiles", null);
//        doGetActiveProfiles.setAccessible(true);
//        doGetActiveProfiles.invoke(environment);

    }

}
